package top.kpromise.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {

    public static Date currentDate(Date date) {
        if (date == null) date = new Date();
        return parseDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date currentDate() {
        return currentDate(new Date());
    }

    public static String currentDateStr() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatHourAndMinute(Date date) {
        return formatDate(date, "HH:mm");
    }

    public static String formatTime(Date date) {
        return formatDate(date, "HH:mm:ss");
    }

    public static String formatDate(Date date, String format) {
        if (date == null) return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return simpleDateFormat.format(date);
    }

    public static Date parseDate(Date date, String format) {
        if (date == null) return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return parseDate(simpleDateFormat.format(date), format);
    }

    public static Date parseDate(String date) {
        return parseDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parseDateNoSend(String date) {
        return parseDate(date, "yyyy-MM-dd HH:mm");
    }

    public static Date parseHourAndMinute(String date) {
        return parseDate(date, "HH:mm");
    }

    public static Date parseTime(String date) {
        return parseDate(date, "HH:mm:ss");
    }

    public static Date parseDate(String date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        try {
            return simpleDateFormat.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addOneMonth(Date date) {
        return addMonth(date, 1);
    }

    public static Date addMonth(Date date, int totalMonthCountToAdd) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, totalMonthCountToAdd);
        return calendar.getTime();
    }

    public static Date addOneDay(Date date) {
        return addDay(date, 1);
    }

    public static Date addDay(Date date, int totalDayCountToAdd) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, totalDayCountToAdd);
        return calendar.getTime();
    }

    public static Date parseBirthday(String date) {
        return parseDate(date, "yyyy-MM-dd");
    }

    public static String formatBirthday(String date) {
        return formatDate(parseBirthday(date), "yyyy-MM-dd");
    }

    public static String formatBirthday(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    public static Date formatBirthdayDate(Date date) {
        return parseDate(date, "yyyy-MM-dd");
    }

    public static int dayInMonth(int year, int month) {
        if (month == 2) {
            return isLeapYear(year) ? 29 : 28;
        }
        //  4， 6， 9，11 30天
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        return 31;
    }

    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
}
